package ita.class1.group2.supermarket.merchant.service;

import ita.class1.group2.supermarket.common.annotation.Autowired;
import ita.class1.group2.supermarket.common.annotation.Component;
import ita.class1.group2.supermarket.common.service.BaseService;
import ita.class1.group2.supermarket.common.util.Md5Utils;
import ita.class1.group2.supermarket.common.util.UUIDUtils;
import ita.class1.group2.supermarket.merchant.dao.UserDao;
import ita.class1.group2.supermarket.merchant.pojo.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class UserService implements BaseService<User, String> {

    @Autowired
    private UserDao userDao;

    @Override
    public User find(String s) {
        return userDao.find(s);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public User save(User user) {
        Map<String,String> condition = new HashMap<>();
        condition.put("username",user.getUsername());
        List<User> list = userDao.findByCondition(condition);
        if(list.size()==0) {
            user.setId(UUIDUtils.getUUID());
            user.setStatus(User.NORMAL);
            user.setPassword(Md5Utils.md5Password(user.getPassword()));
            System.out.println(user.getPassword());
            user.setType(User.MERCHANT_USER);
            return userDao.save(user);
        }
        else {
            return null;
        }

    }

    @Override
    public boolean delete(String s) {
        return userDao.delete(s);
    }

    @Override
    public List<User> findByCondition(Map map) {
        return userDao.findByCondition(map);
    }

    @Override
    public boolean update(User user) {
        return userDao.update(user);
    }


}
